#define _CRT_SECURE_NO_WARNINGS

#include "DoubleLinkedList.h"


DLLNode* DLL_CreateNode(DLL_DataType val)
{
	DLLNode* newnode = malloc(sizeof(DLLNode));
	if (!newnode)
	{
		printf("Error:failed to malloc a new node!\n");
		assert(1);
	}
	newnode->val = val;
	return newnode;
}

DLLNode* DLL_Init()
{
	DLLNode* newnode = DLL_CreateNode(HEAD_VALUE);
	newnode->next = newnode->prev = newnode;
	return newnode;
}

void DLL_PushFront(DLLNode* phead, DLL_DataType val)
{
	assert(phead);
	DLLNode* temp = phead->next;
	phead->next = DLL_CreateNode(val);
	phead->next->prev = phead;
	phead->next->next = temp;
	temp->prev = phead->next;
}

void DLL_PushBack(DLLNode* phead, DLL_DataType val)
{
	assert(phead);
	DLLNode* temp = phead->prev;
	phead->prev = DLL_CreateNode(val);
	phead->prev->next = phead;
	phead->prev->prev = temp;
	temp->next = phead->prev;
}

void DLL_PopFront(DLLNode* phead)
{
	assert(phead);
	if (phead->next == phead)
	{
		return;
	}
	DLLNode* temp = phead->next->next;
	free(phead->next);
	temp->prev = phead;
	phead->next = temp;
}

void DLL_PopBack(DLLNode* phead)
{
	assert(phead);
	if (phead->prev == phead)
	{
		return;
	}
	DLLNode* temp = phead->prev->prev;
	free(phead->prev);
	temp->next = phead;
	phead->prev = temp;
}

DLLNode* DLL_FindNode(DLLNode* phead, DLL_DataType value)
{
	assert(phead);
	DLLNode* current = phead->next;
	for (; current != phead;)
	{
		if (current->val == value)
		{
			return current;
		}
		current = current->next;
	}

	return NULL;
}

int DLL_GetLength(DLLNode* phead)
{
	assert(phead);
	int length = 0;
	DLLNode* current = phead->next;
	for (; current != phead; length++)
	{
		current = current->next;
	}

	return length;
}

void DLL_DestroyList(DLLNode** pphead)
{
	DLLNode* current = (*pphead)->next;
	DLLNode* temp = NULL;
	for (; current != (*pphead);)
	{
		temp = current->next;
		free(current);
		current = temp;
	}
	free(*pphead);
	*pphead = NULL;
}

void DLL_PrintList(DLLNode* phead)
{
	assert(phead);
	DLLNode* current = phead->next;
	printf("Head <-> ");
	for (; current != phead;)
	{
		printf("%d <-> ", current->val);
		current = current->next;
	}
	printf("Head\n");
}

void DLL_InsertAfter(DLLNode* pos, DLL_DataType val)
{
	assert(pos);
	DLLNode* newnode = DLL_CreateNode(val);
	newnode->prev = pos;
	newnode->next = pos->next;
	pos->next->prev = newnode;
	pos->next = newnode;
}

void DLL_InsertBefore(DLLNode* pos, DLL_DataType val)
{
	assert(pos);
	DLLNode* newnode = DLL_CreateNode(val);
	newnode->next = pos;
	newnode->prev = pos->prev;
	pos->prev->next = newnode;
	pos->prev = newnode;
}

void DLL_Erase(DLLNode** pos)
{
	assert(pos);
	if ((*pos)->next == (*pos))
	{
		free(*pos);
		*pos = NULL;
		return;
	}
	DLLNode* temp = *pos;
	(*pos)->next->prev = (*pos)->prev;
	(*pos)->prev->next = (*pos)->next;
	free(temp);
}

void DLL_EraseAfter(DLLNode** pos)
{
	assert(pos);
	if ((*pos)->next == (*pos))
	{
		free(*pos);
		*pos = NULL;
		return;
	}
	DLLNode* temp = (*pos)->next;
	(*pos)->next = (*pos)->next->next;
	(*pos)->next->prev = (*pos);
	free(temp);
}

void DLL_EraseBefore(DLLNode** pos)
{
	assert(pos);
	if ((*pos)->next == (*pos))
	{
		free(*pos);
		*pos = NULL;
		return;
	}
	DLLNode* temp = (*pos)->prev;
	(*pos)->prev = (*pos)->prev->prev;
	(*pos)->prev->next = (*pos);
	free(temp);
}